package thread;


import java.io.ObjectStreamClass;

//懒汉模式
class SingletonLazy {
    //先不着急创建实例
    private static volatile SingletonLazy instance = null;
    private static Object lock = new Object();

    public static  SingletonLazy getInstance() {
        if(instance == null) {//判断是否需要加锁
            synchronized(lock) {
                if (instance == null) {//判断是否需要创建实例
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy() {}
}

public class Demo27 {
    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1 == s2);
    }
}
